<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2051 – Bash doesn't support variables in brace range expansions.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2051 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2051">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="bash-doesnt-support-variables-in-brace-range-expansions">Bash
doesn't support variables in brace range expansions.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2051.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> {1..<span class="va">$n</span>}</span>
<span id="cb1-2"><a href="SC2051.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="cf">do</span></span>
<span id="cb1-3"><a href="SC2051.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$i</span><span class="st">&quot;</span></span>
<span id="cb1-4"><a href="SC2051.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="cf">done</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2051.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> <span class="kw">((</span><span class="va">i</span><span class="op">=</span><span class="dv">1</span><span class="kw">;</span> <span class="va">i</span> <span class="op">&lt;=</span> <span class="va">n</span><span class="kw">;</span> <span class="va">i</span><span class="op">++</span><span class="kw">))</span></span>
<span id="cb2-2"><a href="SC2051.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">do</span></span>
<span id="cb2-3"><a href="SC2051.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$i</span><span class="st">&quot;</span></span>
<span id="cb2-4"><a href="SC2051.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="cf">done</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>In Bash, brace expansion happens before variable expansion. This
means that brace expansion will not account for variables.</p>
<p>For integers, use an arithmetic for loop instead. For zero-padded
numbers or letters, use of eval may be warranted:</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2051.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="va">from</span><span class="op">=</span><span class="st">&quot;a&quot;</span> <span class="va">to</span><span class="op">=</span><span class="st">&quot;m&quot;</span></span>
<span id="cb3-2"><a href="SC2051.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> c <span class="kw">in</span> <span class="va">$(</span><span class="bu">eval</span> <span class="st">&quot;echo {</span><span class="va">$from</span><span class="st">..</span><span class="va">$to</span><span class="st">}&quot;</span><span class="va">)</span><span class="kw">;</span> <span class="cf">do</span> <span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$c</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<p>or more carefully (if <code>from</code>/<code>to</code> could be user
input, or if the brace expansion could have spaces):</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2051.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="va">from</span><span class="op">=</span><span class="st">&quot;a&quot;</span> <span class="va">to</span><span class="op">=</span><span class="st">&quot;m&quot;</span></span>
<span id="cb4-2"><a href="SC2051.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span> <span class="bu">read</span> <span class="at">-d</span> <span class="st">&#39;&#39;</span> <span class="at">-r</span> <span class="va">c</span></span>
<span id="cb4-3"><a href="SC2051.html#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="cf">do</span></span>
<span id="cb4-4"><a href="SC2051.html#cb4-4" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Read </span><span class="va">$c</span><span class="st">&quot;</span></span>
<span id="cb4-5"><a href="SC2051.html#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="cf">done</span> <span class="op">&lt;</span>  <span class="op">&lt;(</span><span class="bu">eval</span> <span class="st">&quot;printf &#39;%s\0&#39; </span><span class="va">$(</span><span class="bu">printf</span> <span class="st">&quot;{%q..%q}.jpg&quot;</span> <span class="st">&quot;</span><span class="va">$from</span><span class="st">&quot;</span> <span class="st">&quot;</span><span class="va">$to</span><span class="st">&quot;</span><span class="va">)</span><span class="st">&quot;</span><span class="op">)</span></span></code></pre></div>
<h3 id="exceptions">Exceptions</h3>
<p>None (if you're writing for e.g. zsh, you can use a directive to
disable this check)</p>
<h3 id="related-resources">Related Resources:</h3>
<ul>
<li><a
href="https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash">StackOverflow:
Variables in bash seq replacement ({1..10})</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


